package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.staff.domain.StaffBaseinfo;
import com.ruoyi.staff.domain.StaffContract;
import com.ruoyi.system.domain.SjMeterReading;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.service.IElecMeterService;

import com.ruoyi.system.service.ISysFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *
 *
 */
@Controller
@RequestMapping("/sujian/elecMeter")
public class ElecMeterController extends BaseController
{
    private String prefix = "propertys/sujian/elec/meter";

    @Autowired
    private IElecMeterService elecMeterService;

    @Autowired
    private ISysFloorService floorInfoService;

    @GetMapping()
    public String meterReading(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/main";
    }
    /**
     * 抄表列表查询
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjMeterReading sjMeterReading)
    {
        startPage();
        List<SjMeterReading> sjMeterReadingList = elecMeterService.getElecMeterList(sjMeterReading);
        return getDataTable(sjMeterReadingList);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/add";
    }

    /**
     * 抄表信息保存
     */
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(SjMeterReading sjMeterReading){
        try {
            SjMeterReading sjMr = elecMeterService.selectElecMeterByFloorAndEtldt(sjMeterReading);
            if(sjMr!=null&&!sjMr.getAmmeterId().equals(sjMeterReading.getAmmeterId())){
                return AjaxResult.error("该月份下已有抄表记录");
            }
            int i = elecMeterService.insertElecMeter(sjMeterReading);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("新增失败："+e.getMessage());
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/edit/{ammeterId}")
    public String toEdit(@PathVariable("ammeterId") String ammeterId, ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        SjMeterReading sjMeterReading = elecMeterService.getElecMeterById(ammeterId);
        modelMap.put("sjMeterReading",sjMeterReading);
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/edit";
    }

    /**
     * 抄表信息修改
     */
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(SjMeterReading sjMeterReading){
        try {
            SjMeterReading sjMr = elecMeterService.selectElecMeterByFloorAndEtldt(sjMeterReading);
            if(sjMr!=null&&!sjMr.getAmmeterId().equals(sjMeterReading.getAmmeterId())){
                return AjaxResult.error("该月份下已有抄表记录");
            }
            int i = elecMeterService.updateElecMeter(sjMeterReading);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("修改失败："+e.getMessage());
        }
        return AjaxResult.error("保存失败");
    }

    /**
     * 抄表信息删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try {
            int i = elecMeterService.removeElecMeter(ids);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败："+e.getMessage());
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 根据楼层和表名判断是否重复
     */
    @PostMapping("/checkFloorAndEtldt")
    @ResponseBody
    public String checkFloorAndEtldt(SjMeterReading sjMeterReading){
        SjMeterReading sjMr = elecMeterService.selectElecMeterByFloorAndEtldt(sjMeterReading);
        if(sjMr!=null&&!sjMr.getAmmeterId().equals(sjMeterReading.getAmmeterId())){
            return "1";
        }else{
            return "0";
        }
    }

    /**
     * 状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SjMeterReading sjMeterReading){
        try {
            elecMeterService.changeElecMeterStatus(sjMeterReading);
        }catch (Exception e){
            return AjaxResult.error("状态修改失败："+e.getMessage());
        }
        return AjaxResult.success();
    }

    //导入抄表数据
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SjMeterReading> util = new ExcelUtil<>(SjMeterReading.class);
        List<SjMeterReading> lists = util.importExcel(file.getInputStream());
        //校验数据
        String message =elecMeterService.importElecMeter(lists,updateSupport);
        return AjaxResult.success(message);
    }


    //导入抄表数据模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SjMeterReading> util = new ExcelUtil<SjMeterReading>(SjMeterReading.class);
        return util.importTemplateExcel("抄表信息数据");
    }
}
